""""""""""""""""""""""""""""""""" 杂项
" 去除空白
nmap wb :%s/\s\+$//ge<CR>
""autocmd BufWritePre *.lua,*.[hcm],*.cpp,*.mm,*.sh,*.py normal wb

" 快速编辑 init.vim
map <leader>e :exe "e! " .. expand($MYVIMRC)<CR>

" 切换相对行号的显示
map <leader>i :set rnu!<CR>

" 调用tablar执行对齐
map <leader>t :Tabularize /

" 查看撤销树
map <leader>h :UndotreeToggle<CR>

" 注释
map <leader>b <Plug>NERDCommenterToggle

" 打开终端并切换到输入模式
if has('unix')
    map <leader>z :sp term://zsh<CR><c-w>J:res 10<CR>A
elseif has('win32')
    map <leader>z :sp term://pwsh<CR><c-w>J:res 10<CR>A
endif
tnoremap <Esc> <C-\><C-n>

" 统计单词个数
vmap <leader>c y:%s/<C-R>0//ng<CR>
nmap <leader>c yiw:%s/<C-R>0//ng<CR>

" open/close nerd tree
nmap wm :NERDTreeToggle<CR>
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
let g:NERDTreeIgnore=['\.pyc$', '\~$', '\.o$']

"映射quickfix的前一个/后一个
map <leader>p :cp<CR>
map <leader>n :cn<CR>

" 使用 p 粘贴内容
set clipboard=unnamedplus

" 头文件和源文件切换
nmap <silent> <leader>a :FSHere<CR>

" 处理减数字为 ctrl-s
nnoremap <C-s> <C-x>

" 取消高亮
nmap <silent> <leader>H :noh<CR>

" win 上后台还不支持，禁用它
if has('win32')
    noremap <C-Z> u
endif

" tagbar
nmap tw :TagbarToggle<CR>

" easy-motion 配置 normal 模式跨窗口检索
nmap s <Plug>(easymotion-overwin-f2)
nmap <leader>l <Plug>(easymotion-overwin-line)

" 让 easy-motion 大小写敏感
let g:EasyMotion_smartcase = 1
let g:EasyMotion_inc_highlight = 0

" 让 easy-tagags 使用 python
let g:easytags_python_enabled = 1
set tags=tags
let g:easytags_dynamic_files = 1
let g:easytags_async = 1
let g:easytags_auto_update = 0

" 用浏览器打开当前文件
if has('mac')
    " 获取当前的浏览器 id
    let s:browser_bundle = trim(system("plutil -p ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist | grep https -b3 | awk 'NR==3 {split($4, arr, \"\\\"\"); print arr[2]}'"))
    function OpenURL(url)
        let cmd = "!open -b ".s:browser_bundle." ".a:url
        exe cmd
    endfunction
    nmap <leader>o :silent call OpenURL("".expand("%:p"))<CR>
elseif has('unix')
    nmap <leader>o :silent !x-www-browser %<CR>
elseif has('win32')
    nmap <leader>o :silent !start msedge file://%:p<CR>
endif

" 格式化
nmap <leader>ff :Autoformat<CR>
""""""""""""""""""""""""""""""""" GIT
" 在每一个 tab 打开指定 git diff --name-only [tags] 中指定的文件
nmap <leader>d :Git difftool -y
nmap <silent> <leader>D :Git difftool -y<CR>

" 在分屏中打开光标下的文件
nmap sgf :wincmd f<CR>
nmap vgf :vertical wincmd f<CR>

" 查看当前文件夹的 git diff 列表
nmap <leader>gdc :Git difftool .<CR>
" 查看当前文件的 git diff
nmap <leader>gd :Gvdiffsplit!<CR>
" 3ways 合并的时候使用哪个提交
nmap <leader>dh :diffget //2<CR>
nmap <leader>dl :diffget //3<CR>
" 查看当前文件的修改历史
function ShowCurrentFileGitLog()
    let v = ''.expand("%:p")
    execute("Git log --format='%h %cn [%cr]: %s' %")
    return v
endfunction
nmap <silent> <leader>gl :let g:current_git_log_file = ShowCurrentFileGitLog()<CR>
" 对当前文件执行 git add
nmap <leader>ga :Git add %<CR>
" 显示当前行的具体 blame
nmap <leader>gb :Gitsigns blame_line<CR>
nmap <leader>gB :exec 'Git blame -L '.line('.')<CR>
" 查看当前commit对应当前文件的改动
nmap <leader>gsc :exec 'Git show '.expand('<cword>').' -- '.g:current_git_log_file<CR>
" 定位到下一个 git 冲突位置
nmap <leader>gn /<\{7\}<CR>

""""""""""""""""""""""""""""""""" 检索
" 使用 rg 快速检索
set grepprg=rg\ --vimgrep
set grepformat^=%f:%l:%c:%m
" 检索到内容的时候自动打开 quickfix
augroup autoquickfix
    autocmd!
    autocmd QuickFixCmdPost [^l]* cwindow
    autocmd QuickFixCmdPost    l* lwindow
augroup END
function! SearchVisualSelection() range
    let x = getpos("'<")[2]
    let y = getpos("'>")[2]

    let keyword = getline(line('.'))[ x - 1 : y - 1]
    execute("grep ".keyword)
endfunction
command! -range SearchVisualSelection call SearchVisualSelection()
nmap <leader>F :exec 'grep '.expand('<cword>')<CR>
vmap <silent> <leader>F :SearchVisualSelection<CR>
